/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.viettel.bkno.visionone.untils;

import com.viettel.bkno.visionone.dataaccess.DatabaseAccessor;
import com.viettel.bkno.visionone.entities.Role;
import com.viettel.bkno.visionone.entities.User;
import com.viettel.bkno.visionone.exception.CoreServiceException;
import com.viettel.bkno.visionone.services.AdminServices;
import com.viettel.bkno.visionone.services.AgentServices;
import com.viettel.bkno.visionone.services.ServicesManager;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author hien
 */
public class Test {
    static Logger logger = Logger.getLogger(Test.class);
    
    static AdminServices adminServices = new AdminServices();
    static AgentServices agentServices = new AgentServices();
    
    public static void main(String[] args) {
//        testConfigLoad();
//        ServicesManager.getInstance().start();
         testAddUser();
    }
    
    static void testAddUser() {
        try {
            String token = adminServices.getSessionToken("hienpt9", "123456aA@");
            User user = new User();
            user.setUsername("hienphanbk");
            user.setRole(Role.SUB_ADMIN);
            user.setPassword("123456aA@");
            user.setEmail("phanhien.fit@gmail.com");
            
            adminServices.addUser("hienpt9", token, user);
        } catch (CoreServiceException ex) {
            logger.error("Khong add duoc user", ex);
        }
        
    }
    
    static String testLogin(String username, String password) throws CoreServiceException {
        String token = ServicesManager.getInstance().getUserManager().authen(username, password);
        if (token != null) {
            System.out.println("Login successful. Token = "+token);
        }
        return token;
    }
    
    static void testAddRootUser() throws CoreServiceException {
        // Adduser
        User user = new User();
        user.setUsername("hienpt9");
        user.setPassword("123456aA@");
//        try {
//            testAddUser(user);
//        } catch (CoreServiceException ex) {
//            logger.error("Can not add new user", ex);
//        }
        ServicesManager.getInstance().getUserManager().addUser(user);
    }
    
    static void testConnectDatabase() {
        DatabaseAccessor.getInstance();
        try {
            DatabaseAccessor.getInstance().getUserInfo("x", "x");
        } catch (CoreServiceException ex) {
            logger.error("Xac thuc khong thanh cong", ex);
        }
    }
    
    static void testConfigLoad() {
        ConfigurationManager.getInstance().load();
    }
    
    static void testMd5Encrypt() {
        SecurityManager sm = new SecurityManager();
        System.out.println(sm.genMd5("test"));
    }
    
    void test1() {
        FileOutputStream output = null;
        try {
//        String key = KeyGenerator.getInstance().genPairkey();
//        System.out.println(key);
//        AgentServices agent = new AgentServices();
//        //System.out.println(agent.getServerTime());
//        System.out.println(agent.getServerTime());
            Properties config = new Properties();
            output = new FileOutputStream("visionone.cfg");
            // set the properties value
            config.setProperty("database", "localhost");
            config.setProperty("dbuser", "mkyong");
            config.setProperty("dbpassword", "password");
            // save properties to project root folder
            config.store(output, null);
        } catch (FileNotFoundException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                output.close();
            } catch (IOException ex) {
                java.util.logging.Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
